{% extends 'base.html' %}
{% load helpers %}
{% load ui_framework %}

{% block breadcrumbs_wrapper %}{% render_breadcrumbs %}{% endblock %}

{% block extra_styles %}
    <style>
        .panel {
            overflow: hidden  /* Fix for panel overflow issues */
        }
    </style>
{% endblock %}

{% block content %}
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    <strong>Worker Queues</strong>
                </div>
                <table class="table table-hover card-body attr-table">
                    {% for queue_name, workers in worker_status.queue_worker_count.items %}
                        <tr>
                            <td><span class="badge bg-primary">{{ queue_name }}</span></td>
                            <td>
                                {% for worker in workers %}
                                    <span class="badge bg-default">{{ worker }}</span>
                                {% empty %}
                                    No workers found.
                                {% endfor %}
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            </div>
        </div>
        {% for worker in worker_status.workers %}
            <div class="col-lg-6">
                <div class="card">
                    <div class="card-header">
                        <strong>{{ worker.hostname }}</strong>
                    </div>
                    <table id="accordion" class="table table-hover card-body attr-table">
                        <tbody>
                            <tr>
                                <td>Uptime</td>
                                <td colspan="2">{{ worker.uptime|render_uptime }}</td>
                            </tr>
                            <tr>
                                <td>Queues</td>
                                <td colspan="2">
                                    {% for queue in worker.queues %}
                                        <span class="badge bg-primary">{{ queue }}</span>
                                    {% endfor %}
                                </td>
                            </tr>
                            <tr>
                                <th class="position-relative" colspan="{{ worker.running_tasks_table.columns|length }}">
                                    <button
                                        aria-controls="collapseme-running-tasks-{{ worker.hostname|slugify }}"
                                        aria-expanded="false"
                                        class="btn float-none fs-4 stretched-link nb-collapse-toggle"
                                        data-bs-toggle="collapse"
                                        data-bs-target="#collapseme-running-tasks-{{ worker.hostname|slugify }}"
                                        name="running-tasks-{{ worker.hostname|slugify }}"
                                        type="button"
                                    >
                                        <span class="mdi mdi-chevron-down me-4"></span><!--
                                    -->Running Tasks {% badge worker.running_tasks_table.rows|length show_empty=True %}
                                    </button>
                                </th>
                            </tr>
                        </tbody>
                        <tbody class="collapse nb-transition-none" id="collapseme-running-tasks-{{ worker.hostname|slugify }}">
                            <tr>
                                {% for column in worker.running_tasks_table.columns %}
                                    <th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
                                {% endfor %}
                            </tr>
                            {% for row in worker.running_tasks_table.page.object_list|default:worker.running_tasks_table.rows %}
                                <tr {{ row.attrs.as_html }}>
                                    {% for column, cell in row.items %}
                                        <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
                                    {% endfor %}
                                </tr>
                            {% empty %}
                                {% if worker.running_tasks_table.empty_text %}
                                    <tr>
                                        <td colspan="{{ worker.running_tasks_table.columns|length }}" class="text-center text-secondary">
                                            &mdash; {{ worker.running_tasks_table.empty_text }} &mdash;
                                        </td>
                                    </tr>
                                {% endif %}
                            {% endfor %}
                        </tbody>
                        <tbody>
                            <tr>
                                <th class="position-relative" colspan="{{ worker.pending_tasks_table.columns|length }}">
                                    <button
                                        aria-controls="collapseme-pending-tasks-{{ worker.hostname|slugify }}"
                                        aria-expanded="false"
                                        class="btn float-none fs-4 stretched-link nb-collapse-toggle"
                                        data-bs-toggle="collapse"
                                        data-bs-target="#collapseme-pending-tasks-{{ worker.hostname|slugify }}"
                                        name="pending-tasks-{{ worker.hostname|slugify }}"
                                        type="button"
                                    >
                                        <span class="mdi mdi-chevron-down me-4"></span><!--
                                    -->Pending Tasks {% badge worker.pending_tasks_table.rows|length show_empty=True %}
                                    </button>
                                </th>
                            </tr>
                        </tbody>
                        <tbody class="collapse" id="collapseme-pending-tasks-{{ worker.hostname|slugify }}">
                            <tr>
                                {% for column in worker.pending_tasks_table.columns %}
                                    <th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
                                {% endfor %}
                            </tr>
                            {% for row in worker.pending_tasks_table.page.object_list|default:worker.pending_tasks_table.rows %}
                                <tr {{ row.attrs.as_html }}>
                                    {% for column, cell in row.items %}
                                        <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
                                    {% endfor %}
                                </tr>
                            {% empty %}
                                {% if worker.pending_tasks_table.empty_text %}
                                    <tr>
                                        <td colspan="{{ worker.pending_tasks_table.columns|length }}" class="text-center text-secondary">
                                            &mdash; {{ worker.pending_tasks_table.empty_text }} &mdash;
                                        </td>
                                    </tr>
                                {% endif %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            </div>
        {% empty %}
            </div>
            <div class="alert alert-warning">
                {% with increased_timeout=worker_status.timeout|add:5 %}
                    <strong>Warning:</strong> No workers responded within the {{ worker_status.timeout }} second timeout.
                    {% if increased_timeout <= worker_status.max_timeout %}
                        <a href="{{ request.path }}?timeout={{ increased_timeout }}">Retry with a {{ increased_timeout }} second timeout</a>.
                    {% else %}
                        <a href="{{ request.get_full_path }}">Retry</a>.
                    {% endif %}
                {% endwith %}
        {% endfor %}
    </div>
{% endblock %}
